#include "Base.h"
#include "ChessProtocol.h"

void ParseRuler(const char *szRuler, int arrProperty[])
{
	if(szRuler == NULL || *szRuler == '\0')
		return;
	sscanf(szRuler,"%d,%d,%d",
		arrProperty + Property_Round_Time,
		arrProperty + Property_Step_Time,
		arrProperty + Property_IncTimePerStep);
}

BOOL ReqMovePiece::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nMove, sizeof(m_nMove));
	return TRUE;
}

BOOL ReqMovePiece::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nMove, sizeof(m_nMove));
	return TRUE;
}

BOOL AckMovePiece::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nSeat, sizeof(m_nSeat));
	iStream.Read((char*)&m_nMove, sizeof(m_nMove));
	return TRUE;
}

BOOL AckMovePiece::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nSeat, sizeof(m_nSeat));
	oStream.Write((char*)&m_nMove, sizeof(m_nMove));
	return TRUE;
}

BOOL ReqEndGame::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nColor, sizeof(m_nColor));
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	iStream.Read((char*)&m_nResultReason, sizeof(m_nResultReason));
	return TRUE;
}

BOOL ReqEndGame::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nColor, sizeof(m_nColor));
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	oStream.Write((char*)&m_nResultReason, sizeof(m_nResultReason));
	return TRUE;
}

BOOL AckEndGame::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_bAgree, sizeof(m_bAgree));
	return TRUE;
}

BOOL AckEndGame::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_bAgree, sizeof(m_bAgree));
	return TRUE;
}

BOOL ChessResult::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nSeat, sizeof(m_nSeat));
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	iStream.Read((char*)&m_nResultReason, sizeof(m_nResultReason));
	iStream.Read((char*)&m_arrScore, sizeof(m_arrScore));
	return TRUE;
}

BOOL ChessResult::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nSeat, sizeof(m_nSeat));
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	oStream.Write((char*)&m_nResultReason, sizeof(m_nResultReason));
	oStream.Write((char*)&m_arrScore, sizeof(m_arrScore));
	return TRUE;
}

BOOL SCChessSnapshot::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nColor, sizeof(m_nColor));
	iStream.Read((char*)&m_Pieces, sizeof(m_Pieces));
	iStream.Read((char*)&m_nNotEatStepCount, sizeof(m_nNotEatStepCount));
	iStream.Read((char*)&m_arrRoundTime, sizeof(m_arrRoundTime));
	iStream.Read((char*)&m_arrStepTime, sizeof(m_arrStepTime));
	int nCount;
    iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
        HistoryBoard *pHistoryBoard = new  HistoryBoard;
		pHistoryBoard->Read(iStream);
		m_listHistory.AddTail(pHistoryBoard);
	}
	return TRUE;
}

BOOL SCChessSnapshot::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nColor, sizeof(m_nColor));
	oStream.Write((char*)&m_Pieces, sizeof(m_Pieces));
	oStream.Write((char*)&m_nNotEatStepCount, sizeof(m_nNotEatStepCount));
	oStream.Write((char*)&m_arrRoundTime, sizeof(m_arrRoundTime));
	oStream.Write((char*)&m_arrStepTime, sizeof(m_arrStepTime));
	int nCount = m_listHistory.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		m_listHistory[i]->Write(oStream);
	}
	return TRUE;
}

